<?php

class Links {

    private $link_start; //для ссылок на товары
    private $link_end;
    private $paginator_start; // для пагинатора
    private $paginator_end;
    private $page_links;
    private $page_names;
    private $domain;
    private $last;
    private $log_file;

    public function __construct($link_start, $link_end, $paginator_start = "", $paginator_end = "") {
        $this->page_links = array();
        $this->page_names = array();
        $this->last = array();
        $this->link_start = $this->addSlashes(trim($link_start));
        $this->link_end = $this->addSlashes(trim($link_end));
        $this->paginator_start = $this->addSlashes(trim($paginator_start));
        $this->paginator_end = $this->addSlashes(trim($paginator_end));
        $this->log_file = 'Parser.txt';
    }

    #Экранирует спец символы для регулярок

    private function addSlashes($str) {
        $search = array(" ", "#", ".", "^", "(", ")", '"', "'", "-", "[", "]", "*", "+", "|", "?", "$", "{skip}");
        $replace = array("\s", "\#", "\.", "\^", "\(", "\)", '\"', "\'", "\-", "\[", "\]", "\*", "\+", "\|", "\?", "\$", ".*");
        return str_replace($search, $replace, $str);
    }

    #смотрит какая кодировка у полученной страницы и изменяет её на утф8

    private function convertCharset($string) {
        $reg_exp_charset = '#\<meta[^<>]+charset\s*\=\s*([\w\-]+)#';
        if (preg_match($reg_exp_charset, $string, $m)) {
            $string = iconv($m[1], 'utf-8', $string);
        }
        return $string;
    }

    #формирует ссылки на товары

    public function getTovarLinks() {
        $links = array();
        $i = 0;
        //print_r($this->page_links['url']); die();
        foreach ($this->page_links['url'] as $cat_url) {
            $cat_name = $this->page_links['name'][$i];
            $reg_exp_link = '#' . $this->link_start . '(.*)' . $this->link_end . '#Ui';
            $rc = new RollingCurl();
            $rc->get($cat_url);
            $codePage = $rc->execute();
            $codePage = $this->convertCharset($codePage);
            if (preg_match_all($reg_exp_link, $codePage, $match)) {
                foreach ($match[1] as $m) {
                    $m = str_replace("amp;", "", $m);
                    if (substr($m, 0, 1) != '/')
                        $m = '/' . $m;
                    if (!strpos($m, $this->domain))
                        $m = $this->domain . $m;
                    if (!in_array($m, $links))
                        $links[] = $m . ";" . $cat_name;
                }
            }
            $i++;
        }
        $links = array_unique($links);
        file_put_contents(dirname(__File__) . DIRECTORY_SEPARATOR . "tovar_links.txt", implode("\r\n", $links));
        echo "ok";
    }

    #бегалка по пагинатору собирает все ссылки на категории

    private function parsePaginator($start_url, $cat_url, $cat_name) {
        $this->last[] = $start_url;
        $reg_exp_pag = '#' . $this->paginator_start . '(.*)' . $this->paginator_end . '#Uis';
        $this->domain = "http://" . parse_url($start_url, PHP_URL_HOST);  
        $codePage = Curl::get($cat_url);
        $codePage = $this->convertCharset($codePage);
        if (preg_match($reg_exp_pag, $codePage, $match1)) {           
            if (preg_match_all('#<a.*href="(.*)"#Uis', $match1[1], $ma)) {
                foreach ($ma[1] as $hz) {
                    $hz = str_replace("amp;", "", $hz);
                    if (!strpos($hz, $this->domain)) { //если ссылка относительная
                        if (substr($hz, 0, 1) == '?') { //если у ссылки вначале вопрос тогда лепим к ней урл с категорией
                            $hz = $cat_url . $hz;
                        } elseif (substr($hz, 0, 1) != '/') { // если не вопрос и не палка то лепим палку и домен
                            $hz = '/' . $hz;
                            $hz = $this->domain . $hz;
                        } else {
                            $hz = $this->domain . $hz; // если палка то просто домен
                        }
                    }

                    if (!in_array($hz, $this->page_links['url'])) {
                        $this->page_links['url'][] = $hz;
                        $this->page_links['name'][] = $cat_name;
                    } else {
                        continue;
                    }
                }               
                $last = $this->page_links['url'][count($this->page_links['url']) - 1];
                if (!in_array($last, $this->last)) {
                    $this->parsePaginator($last, $cat_url, $cat_name);
                }
            }
        }
    }

    public static function uploadCatFile() {
        $file = "category_links.txt";
        $uploaded_file = $_FILES['catfile']['tmp_name'];
        if (move_uploaded_file($uploaded_file, $file)) {
            echo "success";
        } else {
            echo "error";
        }
    }

    #получает список категорий из файла

    public function getCatLinks() {
        $file = file_get_contents(dirname(__File__) . DIRECTORY_SEPARATOR ."category_links.txt");
//        $enc = mb_detect_encoding($str, "auto");
//        $file = iconv($enc, "UTF-8", $file);
        $cat_links = array_unique(explode("\r\n", $file));
        foreach ($cat_links as $cat_url) {
            if (preg_match('#(.+)\s(http://.+)#i', $cat_url, $m)) {
                $this->page_links['name'][] = $m[1];
                $this->page_links['url'][] = $m[2];
                $this->parsePaginator($m[2], $m[2], $m[1]);
            }
        }
    }

    public static function viewTovarLinks() {
        $tovar_links = explode("\r\n", file_get_contents(dirname(__File__) . DIRECTORY_SEPARATOR ."tovar_links.txt"));
        foreach ($tovar_links as $tovar_link) {
            $tovar_link = reset(explode(";", $tovar_link));
            echo "<pre>";
            print_r($tovar_link);
            echo "</pre>";
        }
    }

}

?>
